<?php

namespace App\Http\Controllers\Login;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\AdminModels\Message;
use App\Models\PartnerModels\Partner;
use App\Models\AdminModels\PartnerMessage;

class MessageController extends Controller
{
    public function index()
    {
        return view('login/message/index', ['admin_message_menu' => true]);
    }
    //创建消息页面
    public function create()
    {
        $oPartner = new Partner();
        $partners = $oPartner->getAllData();
        return view('login/message/create', ['admin_message_menu' => true, 'partners' => $partners]);
    }
    //消息保存
    public function createSave(Request $request)
    {
        $oPartner = new Partner();
        $params = $request->all();
        if (isset($params['title']) && !empty($params['title']) && isset($params['content']) && !empty($params['content'])) {
            $oMessage = new Message();
            $data = [];
            $data['title'] = trim($params['title']);
            $data['content'] = trim($params['content']);
            $data['send_name'] = env('SYSTEM_NAME') . '平台';
            $data['admin_id'] = $request->session()->get('user')[0]['admin_id'];
//            $data['receive_ids'] = empty($params['receive_ids']) ? '' : json_encode($params['receive_ids']);
            $data['type']=isset($params['type'])? $params['type']:0;
            $nowTime = date('Y-m-d H:i:s');
            $data['create_time'] = $nowTime;
            $data['update_time'] = $nowTime;
          //  print_r($data);exit();
            $mid = $oMessage->db_insertGetId($data);
            if ($mid) {
                $oPartnerMessage = new PartnerMessage();
                if ($data['type']=0) {
                     $partners = $oPartner->getAllData();
                    if (!empty($partners)) {
                        foreach ($partners as $partner) {
                            $pmData = [];
                            $pmData['partner_id'] = $partner['id'];
                            $pmData['message_id'] = $mid;
                            $pmData['create_time'] = $nowTime;
                            $oPartnerMessage->db_insert($pmData);
                        }
                    }
                } else {
                    $partners = $oPartner->getTypeData($data['type']);
                    if (!empty($partners)) {
                        foreach ($partners as $partner) {
                            $pmData = [];
                            $pmData['partner_id'] = $partner['id'];
                            $pmData['message_id'] = $mid;
                            $pmData['create_time'] = $nowTime;
                            $oPartnerMessage->db_insert($pmData);
                        }
                    }
                }
            }
            return redirect('admin/message/list');
        }
        abort(404);
    }

    public function edit($id)
    {
        if (intval($id) > 0) {
            $oMessage = new Message();
            $data = $oMessage->db_get(['id' => ['=', $id]]);
            if (!empty($data)) {
                return view('login/message/edit', ['admin_message_menu' => true, 'data' => $data]);
            }
        }
        abort(404);
    }

    public function editSave($id, Request $request)
    {
        $oMessage = new Message();
        $mData = $oMessage->db_get(['id' => ['=', $id]]);
        if (!empty($mData)) {
            $params = $request->all();
            if (isset($params['title']) && !empty($params['title']) && isset($params['content']) && !empty($params['content'])) {
                $data = [];
                $data['title'] = trim($params['title']);
                $data['content'] = trim($params['content']);
                $data['update_time'] = date('Y-m-d H:i:s');
                $oMessage->db_update('', $id, $data);
                return redirect('admin/message/list');
            }
        }
        abort(404);
    }

    public function view($id)
    {
        if (intval($id) > 0) {
            $oMessage = new Message();
            $data = $oMessage->db_get(['id' => ['=', $id]]);
            if (!empty($data)) {
                $data['receive_name'] = '全部';
                if (!empty($data['receive_ids'])) {
                    $data['receive_name'] = '';
                    $oPartner = new Partner();
                    $receive_ids = json_decode($data['receive_ids'], true);
                    foreach ($receive_ids as $receive_id) {
                        $partnerData = $oPartner->db_get(['id' => ['=', $receive_id]]);
                        $data['receive_name'] .= !empty($partnerData) ? ($partnerData['user_name'] . '【' . $partnerData['name'] . ' ' . $partnerData['mobile'] . '】,') : '';
                    }
                }
                $oAdmin = new \App\Models\AdminModels\Admin();
                $adminData = $oAdmin->db_get(['admin_id' => ['=', $data['admin_id']]]);
                $data['admin_name'] = !empty($adminData) ? $adminData['admin_name'] : '';
                return view('login/message/view', ['admin_message_menu' => true, 'data' => $data]);
            }
        }
        abort(404);
    }


}